package com.example.gobang.api;


import com.example.gobang.game.OnlineUserManager;
import com.example.gobang.model.User;
import com.example.gobang.model.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@RestController
public class UserApi {

    @Resource
    private UserMapper userMapper;
    @Autowired
    private OnlineUserManager onlineUserManager;
    private final static PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    /**
     * 用户登入处理
     * @param username
     * @param password
     * @param request
     * @return
     */
    @PostMapping("/login")
    public Object login(String username, String password, HttpServletRequest request){
        //查询用户信息
        User user = userMapper.selectByName(username);
        if (user == null || !passwordEncoder.matches(password, user.getPassword())) {
            System.out.println("登录失败");
            return new User();
        }
        //判断账号多开
        WebSocketSession fromGameHall = onlineUserManager.getFromGameHall(user.getUserId());
        if (fromGameHall != null){
            System.out.println("账号在别处登入！");
            User resUser = new User();
            resUser.setUserId(-1);
            return resUser;
        }
        //登入成功，将用户信息存入session
        HttpSession session = request.getSession(true);
        session.setAttribute("user",user);
        return user;
    }

    /**
     * 用户注册
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/register")
    public Object register(String username,String password){
        try {
            User user = new User();
            user.setUsername(username);
            String encodedPassword = passwordEncoder.encode(password);
            user.setPassword(encodedPassword);
            userMapper.insert(user);
            return user;
        } catch (DuplicateKeyException e) {
            return new User();
        }
    }

    /**
     * 获取当前登入用户的信息
     * @param request
     * @return
     */
    @GetMapping("/userInfo")
    public Object getUserInfo(HttpServletRequest request){
        try {
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute("user");
            user = userMapper.selectByName(user.getUsername());
            return user;
        }catch (NullPointerException e){
            return new User();
        }
    }

    public static void main(String[] args) {
        String test = "666";
        String encode = passwordEncoder.encode(test);
        System.out.println(encode);
        //验证密码
        boolean matches = passwordEncoder.matches(test, "$2a$10$fG34.ahPi6itUBCgtoMZB.yILxfCLvjqE6qZxyXAyQatp/vTGWST.");
        System.out.println(matches);

    }

}
